#!/bin/bash

# This script should be run after the data files have been initialized _and_
# membership.properties files have been copied to the nodes.

input_dir=/mnt/sdb/input
output_dir=/mnt/sdc/output
bdb_dir=/mnt/sdd/bdb
bdb_backup_dir=/mnt/sdd/bdb-backup

elven_dir=/home/elven
lookout_dir=/home/lookout
java_base=com.citusdata.elven.feeder

carid_max=1535084
hostname=`GET http://169.254.169.254/latest/meta-data/public-hostname`
placement=`GET http://169.254.169.254/latest/meta-data/placement/availability-zone`
historical_file=/mnt/sdb/historical/historical-tolls.out
membership_file=config/membership.properties

lookout_default="none"
lookout_master=${lookout_default}
historical_options="-DBdbDatastore.Transactional=false"
linearroad_options="-Xms10g -Xmx10g -XX:NewSize=3g -XX:MaxNewSize=3g -XX:+UseConcMarkSweepGC"

if [ $# -ge 2 ]; then
  # cluster_count is the total number of EC2 nodes divided by the replication
  # factor. For example, if the replication factor is 2 and there are 10 EC2
  # nodes in the membership list, cluster_count is 5.
  cluster_count=$1
  lr_factor=$2

  if [ $# -ge 3 ]; then lookout_master=$3; fi
  if [ $# -ge 4 ]; then historical_options=$4; fi
  if [ $# -ge 5 ]; then linearroad_options=$5; fi

else
  echo -n "Usage: process_startup.sh cluster_count lr_factor"
  echo -n " [lookout_master]=${lookout_master}"
  echo -n " [historical_options]=\"${historical_options}\""
  echo -n " [linearroad_options]=\"${linearroad_options}\""
  echo
  exit 1
fi

# Nodes contain data for lr_factor_max expressways; this represents the total
# amount of data that can be handled by servers in one replication group. Since
# each replication group contains two servers, two clients share the data load.
# In other words, each client feeds half of lr_factor_max expressways. The
# following check ensures that this factor is not exceeded.

replication_factor=2
lr_factor_max=10
lr_factor_client_max=$((${lr_factor_max} / ${replication_factor}))
lr_factor_client=$((${lr_factor} + 1))  # Data for xway=1 is ignored, add one.

if [ ${lr_factor_client} -gt ${lr_factor_client_max} ]; then
  echo "Error: Clients can feed at most ${lr_factor_client_max} expressways"
  exit 1
fi

# During each Linear Road benchmark run, toll notifications are appended to the
# BDB JE database. Therefore, BDB files need to be "clean installed" before each
# run. To improve performance, this script backs up BDB files the first time the
# script is run, and re-copies files from the backup upon consequent runs.

if [ -d ${bdb_backup_dir} ]; then
  rm -rf ${bdb_dir}
  cp -r ${bdb_backup_dir} ${bdb_dir}
else
  # First, convert historical files for distributed data feeding
  cd ${elven_dir}
  java ${java_base}.HistoricalDataEc2Converter \
    ${historical_file} ${carid_max} ${cluster_count} ${hostname} ${membership_file}

  # Then, feed previously converted historical files into BDB JE database files
  cd ${elven_dir}
  java ${historical_options} ${java_base}.HistoricalDataFeeder \
    ${historical_file}.ec2 ${bdb_dir}

  cp -r ${bdb_dir} ${bdb_backup_dir}
fi


# If the user specified monitoring servers that store node statistics, start the
# monitoring script
if [ ${lookout_master} != ${lookout_default} ]; then
  cd ${lookout_dir}
  perl system_stats.pl ${lookout_master} < /dev/null >& remote.out &
fi

# Create symlink files in accordance with the number of expressways (L-factor)
# used in linear road tests. These symlinks are created for clients to feed
# data, and this data is split evenly among the two clients in one replication
# group.

direction_factor=2
linearroad_dir=linearroad
mkdir ${input_dir}/${linearroad_dir}
mkdir ${output_dir}/${linearroad_dir}

if [[ ${placement} == "us-east-1a" || ${placement} == "us-east-1c" ]]; then
  symlink_start=0
  symlink_end=$((${lr_factor_client} * ${direction_factor}))
else
  symlink_start=$((${lr_factor_client_max} * ${direction_factor}))
  symlink_end=$(( ${symlink_start} + (${lr_factor} * ${direction_factor}) ))
fi

symlink_index=0
input_files=${input_dir}/*
for input_file in ${input_files}
do
  if [[ ${symlink_start} -le ${symlink_index} && 
        ${symlink_index} -lt ${symlink_end} ]]; then
    ln -s ${input_file} ${input_dir}/${linearroad_dir}
  fi
  ((symlink_index++))
done

# Run the linear road server; the node is now ready to accept data.
cd ${elven_dir}
java ${linearroad_options} ${java_base}.LinearRoadServer ${bdb_dir} \
  < /dev/null >& remote.out &
